<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .Ball {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        background-color: rebeccapurple;
      }
    </style>
  </head>
  <body>
    <div id="app"></div>
    <script src="./libs/react.js"></script>
    <script src="./libs/react-dom.js"></script>
    <script src="./libs/babel.min.js"></script>
    <script type="text/babel">
      const { Component } = React;

      class Ball extends Component {
        componentWillMount() {
          console.log("组件Ball将要挂载");
        }
        componentDidMount() {
          console.log("组件Ball挂载完成");
        }
        componentWillUnmount() {
          console.log("组件Ball将要卸载");
        }
        render() {
          return <div className="Ball"></div>;
        }
      }

      class App extends Component {
        constructor(props) {
          super(props);
          this.state = {
            title: "标题",
            list: ["小白", "李好好", "大堆堆"],
            count: 0,
          };
        }
        componentWillMount() {
          console.log("组件将要挂载");
        }
        componentDidMount() {
          console.log("组件挂载完成");
        }
        componentWillReceiveProps() {
          console.log("组件将要接收动态属性");
        }
        shouldComponentUpdate(p, s) {
          console.log(p);
          console.log(s);
          console.log("组件是否需要更新");
          if (s.count % 2 === 0) {
            return true;
          }
          return true;
        }
        componentWillUpdate() {
          console.log("组件将要更新");
        }
        componentDidUpdate() {
          console.log("组件更新完成");
        }
        componentWillUnmount() {
          console.log("组件将要卸载");
        }
        render() {
          console.log("render执行了");
          return (
            <div>
              <h4
                onClick={() =>
                  this.setState({ title: this.state.title + Math.random() })
                }
              >
                {this.state.title}
              </h4>
              <button
                onClick={() => this.setState({ count: this.state.count + 1 })}
              >
                {this.state.count}
              </button>
              {this.state.count % 2 === 0 ? <></> : <Ball />}
            </div>
          );
        }
      }
      ReactDOM.render(<App />, document.getElementById("app"));
    </script>
  </body>
</html>
